var path = require('path')
    , express = require('express')
    , consolidate = require('consolidate')
    , envConf = require(path.resolve(__dirname, 'config', 'env.conf'))[process.env.run_mode]
    , proxyMiddleware = require(path.resolve(__dirname, 'middleware', 'proxy.middleware'))
    ;

(function (port, hostname) {

    var app = express();

    app.use(proxyMiddleware);

    // set static dir
    // access demo: http://127.0.0.1:8080/static/images/xxx.png
    app.use('/static', express.static(path.resolve(__dirname, 'static')));

    // set views dir
    app.set('views', path.resolve(__dirname, 'views'));
    // set .html as the default extension
    app.set('view engine', 'html');
    // assign the swig engine to .html files
    app.engine('html', consolidate.swig);

    app.use('/', require('./routers/index.js'));

    app.use(function (req, res, next) {
        res.status(404).send('Sorry, can\'t find path "' + req.path + '" !<br><a href="http://haoxx.top">点击跳转到首页</a>');
    });

    app.use(function (err, req, res, next) {
        console.error(err.stack);
        res.status(500).send(err.stack);
    });

    app.listen(port, hostname, function () {
        console.log('app listening at http://%s:%s', hostname, port);
    });

})(envConf.port, envConf.hostname);